package com.cy.stone.system.application.impl;

import com.cy.stone.common.constant.Constant;
import com.cy.stone.common.errorcode.SysErrorCode;
import com.cy.stone.system.application.DictTypeApi;
import com.cy.stone.system.domain.clientobject.dict.DictTypeCO;
import com.cy.stone.system.domain.dto.dict.DictTypeDTO;
import com.cy.stone.system.infrastructure.database.entity.DictTypeEntity;
import com.cy.stone.system.infrastructure.database.service.IDictTypeService;
import com.feiniaojin.gracefulresponse.GracefulResponseException;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryWrapperAdapter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

import static com.cy.stone.system.infrastructure.database.table.DictTypeTableDef.DICT_TYPE_ENTITY;

/**
 * 字典类型接口实现
 * @author Wings
 * @since 2023-12-18
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class DictTypeApiImpl implements DictTypeApi {

    private final IDictTypeService dictTypeService;

    @Override
    public List<DictTypeDTO> list() {
        return dictTypeService.listAs(QueryWrapper.create()
                .select(DICT_TYPE_ENTITY.DICT_TYPE, DICT_TYPE_ENTITY.DICT_NAME)
                .from(DICT_TYPE_ENTITY)
                .where(DICT_TYPE_ENTITY.STATUS.eq(Constant.STATUS_0)), DictTypeDTO.class);
    }

    @Override
    public void addDictType(DictTypeCO dictTypeCO) {
        // 校验类型代码或者类型名称是否已经存在
        Optional<DictTypeEntity> dictType = dictTypeService.getOneOpt(QueryWrapper.create()
                .from(DICT_TYPE_ENTITY)
                .where(DICT_TYPE_ENTITY.DICT_TYPE.eq(dictTypeCO.getDictType()))
                .or(DICT_TYPE_ENTITY.DICT_NAME.eq(dictTypeCO.getDictName())));
        if (dictType.isPresent()) {
            log.error(dictTypeCO.getDictType() + "-" + dictTypeCO.getDictName() + "的字典类型码或者类型名称已经存在");
            throw new GracefulResponseException(SysErrorCode.DICT_TYPE_EXIST.getCode(), SysErrorCode.DICT_TYPE_EXIST.getMessage());
        } else {
            DictTypeEntity dictTypeEntity = DictTypeCO.toAddEntity(dictTypeCO);
            dictTypeService.save(dictTypeEntity);
        }
    }

    @Override
    public void editDictType(DictTypeCO dictTypeCO) {
        // 校验编辑的字典类型是否存在
        Optional<DictTypeEntity> dictType = dictTypeService.getByIdOpt(dictTypeCO.getId());
        if (dictType.isPresent()) {
            DictTypeEntity dictTypeEntity = dictType.get();
            dictTypeEntity.setDictType(dictTypeCO.getDictType());
            dictTypeEntity.setDictName(dictTypeCO.getDictName());
            dictTypeService.updateById(dictTypeEntity);
        } else {
            log.error("ID:" + dictTypeCO.getId() + "字典类型不存在");
            throw new GracefulResponseException(SysErrorCode.DICT_TYPE_NOT_EXIST.getCode(), SysErrorCode.DICT_TYPE_NOT_EXIST.getMessage());
        }
    }
}
